﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ECS.DAL.Entity;
using ECS.DAL.Component;
using ECS.Framework;

namespace E_ICS.E_ICS
{
    public partial class frmStattionSetup2 : Form
    {
        #region private properties
        EicsSetupComponent setup = null;
        List<ObjectContext> setupList = new List<ObjectContext>();
        #endregion

        #region windows event
        public frmStattionSetup2()
        {
            InitializeComponent();
        }
        private void frmStattionSetup2_Load(object sender, EventArgs e)
        {
            try
            {
                setup = new EicsSetupComponent();
                EicsSetup param = new EicsSetup();
                param.SetupId = string.Empty;
                param.AppId = GlobalVariable.user.ApplicationID;
                param.Device = GlobalVariable.user.Device;
                param.UserId = GlobalVariable.user.UserID;
                setupList = setup.Search(param);
                SortableBindingList<EicsSetup> tempList = new SortableBindingList<EicsSetup>();
                foreach (ObjectContext item in setupList)
                {
                    ((EicsSetup)item).AppId = GlobalVariable.user.ApplicationID;
                    ((EicsSetup)item).Device = GlobalVariable.user.Device;
                    ((EicsSetup)item).UserId = GlobalVariable.user.UserID;
                    tempList.Add((EicsSetup)item);
                }
                dgvSetup.AutoGenerateColumns = false;
                dgvSetup.DataSource = tempList;
                dgvSetup.Columns[0].ReadOnly = true;
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        

        private void frmStattionSetup2_FormClosing(object sender, FormClosingEventArgs e)
        {

            DialogResult result;
            try
            {
                foreach (EicsSetup item in setupList)
                {
                    if (item.GetObjectSate() != OBJECT_STATE.None)
                    {
                        result = CommonMessageBox.displayConfirmSaveChanged();
                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {
                            setup.SaveChanges(setupList);
                        }
                        else if (result == System.Windows.Forms.DialogResult.Cancel)
                        {
                            e.Cancel = true;
                        }
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        

        private void dgvSetup_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 5 && !string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    //number
                    if (!ECS.Framework.Utilities.IsNumeric(e.FormattedValue))
                    {
                        e.Cancel = true;
                        CommonMessageBox.ShowMessageError("Invalid number format");
                    }
                }
                else if (e.ColumnIndex == 6 && !string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    //date
                    if (!ECS.Framework.Utilities.IsDate(e.FormattedValue))
                    {
                        e.Cancel = true;
                        CommonMessageBox.ShowMessageError("Invalid date format");
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult result;
            try
            {
                foreach (EicsSetup item in setupList)
                {
                    if (item.GetObjectSate() != OBJECT_STATE.None)
                    {
                        result = CommonMessageBox.displayConfirmSaveChanged();
                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {
                            setup.SaveChanges(setupList);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void dgvSetup_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            try
            {
                
                if (e.ColumnIndex == 5) //Number
                {
                    CommonMessageBox.ShowMessageError("Invalid number format");
                }
                else if (e.ColumnIndex == 6) //Date
                {
                    CommonMessageBox.ShowMessageError("Invalid date format");
                }
                else
                {
                    CommonMessageBox.ShowMessageError(e.Exception);
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        
    }
}
